package com.graduation.cdm.funactivity.util;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.widget.ImageView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.graduation.cdm.funactivity.MyApplication;
import com.graduation.cdm.funactivity.R;

/**
 * Created by Administrator on 2016/9/23.
 */

public class xUtilsImageUtils {
    /**
     * 显示图片（默认情况）
     * 不压缩
     * @param imageView 图像控件
     * @param iconUrl   图片地址
     */
    public static void display(final ImageView imageView, String iconUrl) {
        ImageRequest imageRequest = new ImageRequest(iconUrl, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap bitmap) {
                imageView.setImageBitmap(bitmap);
            }
        },0 , 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                imageView.setImageResource(R.mipmap.default1);
            }
        });
        MyApplication.getRequestQueue().add(imageRequest);
    }

    /**
     * 显示头像，第三个参数为true
     *
     * @param imageView  图像控件
     * @param iconUrl    图片地址
     */
    public static void displayHead(final ImageView imageView, String iconUrl){
        ImageRequest imageRequest = new ImageRequest(iconUrl, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap bitmap) {
                imageView.setImageBitmap(bitmap);
            }
        },74 , 74, Bitmap.Config.RGB_565, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                imageView.setImageResource(R.mipmap.default1);
            }
        });
        MyApplication.getRequestQueue().add(imageRequest);
    }

    /**
     * 显示主页图片
     *
     * @param imageView 图像控件
     * @param iconUrl   图片地址
     * @param radius    圆角半径，
     */
    public static void displayHome(final ImageView imageView, String iconUrl) {
        ImageRequest imageRequest = new ImageRequest(iconUrl, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap bitmap) {
                imageView.setImageBitmap(bitmap);
            }
        },100 , 70, Bitmap.Config.RGB_565, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                imageView.setImageResource(R.mipmap.default1);
            }
        });
        MyApplication.getRequestQueue().add(imageRequest);
    }
    /**
     * 显示圆形头像，第三个参数为true
     *
     * @param imageView  图像控件
     * @param iconUrl    图片地址
     * @param isCircluar 是否显示圆形
     */
    public static void displayXianXi(final ImageView imageView, String iconUrl){
        ImageRequest imageRequest = new ImageRequest(iconUrl, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap bitmap) {
                imageView.setImageBitmap(bitmap);
            }
        },675 , 376, Bitmap.Config.RGB_565, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                imageView.setImageResource(R.mipmap.default1);
            }
        });
        MyApplication.getRequestQueue().add(imageRequest);
    }

    public static void displayActFrag(final ImageView imageView, String iconUrl){
        ImageRequest imageRequest = new ImageRequest(iconUrl, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap bitmap) {
                imageView.setImageBitmap(bitmap);
            }
        },675 , 476, Bitmap.Config.RGB_565, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                imageView.setImageResource(R.mipmap.default1);
            }
        });
        MyApplication.getRequestQueue().add(imageRequest);
    }

    //生成圆角图片
    /*public void GetRoundedCornerBitmap(Bitmap bitmap) {
        try {
            Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                    bitmap.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(output);
            final Paint paint = new Paint();
            final Rect rect = new Rect(0, 0, bitmap.getWidth(),
                    bitmap.getHeight());
            final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(),
                    bitmap.getHeight()));
            final float roundPx = 88;
            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(Color.BLACK);
            canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            final Rect src = new Rect(0, 0, bitmap.getWidth(),
                    bitmap.getHeight());
            canvas.drawBitmap(bitmap, src, rect, paint);
            showImage(output);
        } catch (Exception e) {
            showImage(bitmap);
        }
    }*/
}

